package com.fire.social.controller;

import com.fire.social.config.Result;
import com.fire.social.config.Results;
import com.fire.social.entity.node.User;
import com.fire.social.service.FollowService;
import com.fire.social.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;

@Controller
public class FollowController {

    @Autowired
    private UserService userService;
    @Autowired
    private FollowService followService;

    @PostMapping("/follow/followit")
    @ResponseBody
    public Result followIt(@RequestParam("account") String account,
                           @RequestParam("follower") String follower) {
        Long id = followService.createFollow(account, follower);
        if (id != null) {
            return Results.success(id);
        }
        return Results.failure();
    }

    @PostMapping("/follow/unfollow")
    @ResponseBody
    public Result unfollow(@RequestParam("account") String account,
                           @RequestParam("follower") String follower) {
        Long num = followService.deleteFollow(account, follower);
        if (num == 0) {
            return Results.success();
        }
        return Results.failure();
    }


    @GetMapping("/user/follower")
    public String getFans(HttpServletRequest request, Map<String,Object> map) {
        HttpSession session=request.getSession();
        User user =(User) session.getAttribute("user");
        if(user==null){
            return "login";
        }
        followService.findFollowMe(user, map);
        return "userlist";
    }

    @GetMapping("/user/myfollowing")
    public String getMyFollowing(HttpServletRequest request, Map<String,Object> map){
        HttpSession session=request.getSession();
        User user =(User) session.getAttribute("user");
        if(user==null){
            return "login";
        }
        followService.findMyFollow(user, map);
        return "myfollowing";
    }



}
